/**
 * This js file for confProfile.jsp only
 */
var layout;
var txtColorPicker,bgColorPicker;
$(function(){
	$('.easyui-validatebox').validatebox();
	$('.easyui-validatebox[required]').after("<font color='red'> *</font>");
	layout=new dhtmlXLayoutObject("id_win_container","2U");
	layout.forEachItem(function(item){
		item.hideHeader();
	});
	layout.cells("a").setWidth(200);
	layout.cells("a").fixSize(true,true);
	layout.cells("a").attachObject("id_list");
	
	layout.cells("b").attachObject("id_container_right");
	
	layout.attachFooter("id_footer");
	
	showItem("id_general");
	
	// 高亮左侧的菜单选项
    $(".menu-item").click(function(){
        $(".menu-item").css("font-weight","normal");
        $(this).css("font-weight","bold");
    });
    
	txtColorPicker=new dhtmlXColorPicker("id_txt_cp",false,false,true);
	txtColorPicker.loadUserLanguage("my-lang");
	txtColorPicker.setImagePath("<c:url value='/styles/dhx2/imgs/'/>");
	txtColorPicker.init();
	txtColorPicker.setOnSelectHandler(function(c){
		$("#id_textColor").val(c);
		$("#id_textColor").css("background-color",c);
	});
	$("#id_textColor").click(function(){
		txtColorPicker.show();
	});
	
	bgColorPicker=new dhtmlXColorPicker("id_bg_cp",false,false,true);
	bgColorPicker.loadUserLanguage("my-lang");
	bgColorPicker.setImagePath("<c:url value='/styles/dhx2/imgs/'/>");
	bgColorPicker.init();
	bgColorPicker.setOnSelectHandler(function(c){
		$("#id_textBgColor").val(c);
		$("#id_textBgColor").css("background-color",c);
	});
	$("#id_textBgColor").click(function(){
		bgColorPicker.show();
	});
	
	var opt="${param.opt}";
	
	if(opt=="new"){// init new page values
		$("select[name=bandwidth]").val("1024");
		$("input[name=callInType][value=all]").attr("checked",true);
		$("input[name=encrypt][value=disable]").attr("checked",true);
		$("input[name=h263cif4Enabled]").attr("checked",true);
		$("input[name=h264Enabled]").attr("checked",true);
		$("select[name=h264Definition]").val("HD720P");
		$("input[name=h239Enabled]").attr("checked",true);
		$("select[name=h239Bandwidth]").val("128");
		$("select[name=h239Codec]").val("H.263");
		
		$("input[name=audioFormatsArray]").each(function(){
			this.checked=true;
		});
		
		$("select[name=textPosition]").val("bottom_center");
		$("select[name=textFont]").val("middle");
		
		$("select[name=broadcastBandwidth]").val("384");
	}else if(opt=="modify"){// init modify page values
		$("select[name=bandwidth]").val("${model.bandwidth}");
		$("input[name=callInType][value=${model.callInType}]").attr("checked",true);
		$("input[name=encrypt][value=${model.encrypt}]").attr("checked",true);
		if("${model.h263cif4Enabled}"=="true"){
			$("input[name=h263cif4Enabled]").attr("checked",true);
		}
		if("${model.h264Enabled}"=="true"){
			$("input[name=h264Enabled]").attr("checked",true);
		}
		$("select[name=h264Definition]").val("${model.h264Definition}");
		if("${model.h239Enabled}"=="true"){
			$("input[name=h239Enabled]").attr("checked",true);
		}
		if("${model.muteOnJoin}"=="true"){
			$("input[name=muteOnJoin]").attr("checked",true);
		}
		$("select[name=h239Bandwidth]").val("${model.h239Bandwidth}");
		$("select[name=h239Codec]").val("${model.h239Codec}");
		
		$("input[name=audioFormatsArray]").each(function(){
			if(",${model.audioFormats},".indexOf(","+this.value+",")!=-1){
				this.checked=true;
			}
		});
		
		$("select[name=textPosition]").val("${model.textPosition}");
		$("select[name=textFont]").val("${model.textFont}");
		
		$("#id_textColor").val("${model.textColor}");
		$("#id_textColor").css("background-color","${model.textColor}");		
		
		if("${model.textBgEnabled}"=="true"){
			$("input[name=textBgEnabled]").attr("checked",true);
			$("#id_textBgColor").val("${model.textBgColor}");
			$("#id_textBgColor").css("background-color","${model.textBgColor}");
		}
		
		if("${model.broadcastEnabled}"=="true"){
			$("input[name=broadcastEnabled]").attr("checked",true);
		}
		$("select[name=broadcastBandwidth]").val("${model.broadcastBandwidth}");
	}
	
});

function doSubmit(){
	$('#id_form').form('submit', {
        url:"<c:url value='/app/manage/profile'/>",
        onSubmit: function(){
        	if($('.easyui-validatebox').validatebox("isValid")==false){
        		return false;
        	}else if(isExist()){
        		alert(messages.getMsg("error.exist",[$("#id_profileName").val()]));
        		return false;
        	}else{
				return true;
			}
        },
        success:function(data){
			//alert(data);
			parent.confProfileViewObj.reload();
			parent.closeWin();
        }
	});
}

function isExist(){
	var ret=false;
	var _key=$("#id_profileName").val();
	var _preKey="<c:out value='${model.profileName}'/>";
	
	if("${param.opt}"=="modify"&&_key==_preKey){//whether is the same key in the modify page
		ret=false;
	}else{
		$.ajax({
			url:"<c:url value='/app/manage/profile/check'/>",
			async:false,
			type:"GET",
			data:{
				key:$("#id_profileName").val(),
				times:new Date().toString()
			},
			success:function(data){
				if(data=="true")
					ret=true;
			}
		});
	}
	
	return ret;
}

function showItem(id){
	var items=["id_general","id_auv","id_osd","id_broadcast"];
	$.each(items,function(){
		if(this==id){
			document.getElementById(this).style.display="block";
		}else{
			document.getElementById(this).style.display="none";
		}
	});
}